home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.lib;
-
- import sub_arctic.output.style_manager;
- import sub_arctic.output.style;
- import sub_arctic.output.color_pair;
- import sub_arctic.output.drawable;
- import sub_arctic.constraints.std_function;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.util.Vector;
-
-
- /**
- * This class implements a list item which can display a text string.
- * It is basically a label which knows how to change its display based
- * being selected.
- *
- * @author Ian Smith
- */
- public class text_list_element extends label implements list_element {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * We store whether we are highlighted or not. Initially, we aren't.
- */
- protected boolean _highlighted=false;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a text_list_element with a string. This will use the
- * default font.
- *
- * @param String text the text display.
- */
- public text_list_element(String text)
- {
- this(text,null);
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a text_list_element with a string & a font. If you supply
- * null for the font, you will get the system's default font.
- *
- * @param String text the text display.
- * @param Font font the font to use (or null for the system default font).
- */
- public text_list_element(String text, Font font)
- {
-
- super(text, font==null ? style_manager.default_font() : font);
-
- Color fg=style_manager.default_color_scheme().foreground();
- Color bg=style_manager.default_color_scheme().text_background();
- color_pair cp=new color_pair(fg,bg);
-
- /* set things up to look like the style expects */
- set_draw_colors(cp);
- set_boxed(false);
- set_opaque(true);
-
- /* make sure we exactly enough room for a border */
- set_h_spacing(2);
- set_above_spacing(2);
- set_below_spacing(manager.get_metrics(font()).getDescent()+2);
-
- /* now constrain our width to be the same as our parent */
- set_w_constraint(std_function.offset(PARENT.W(),0));
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Implement the highlight behavior for this list element.
- */
- public void highlight() {
- _highlighted=true;
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Implement the unhighlight behavior for this list element.
- */
- public void unhighlight() {
- _highlighted=false;
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Return the convenient representation of this object. <P>
- *
- * This is a convenient way to use subclassing with listboxes. If
- * you return an object of your own from this method, you will
- * receive it when you ask for listbox contents or selected
- * sets. This lets you easily get at application semantics
- * in your code when using listboxes that display "strings". E.g.
- * if you are displaying a list of filenames in the listbox,
- * you might want to return File objects from this method so
- * when the user selects a file, you won't have to go looking
- * for that File object again.<P>
- *
- * @return Object a string with the text of this object in it
- */
- public Object convert_to_object() {
- return text();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Draw this object on the drawable provided.
- * @param drawable d the drawable to draw on.
- */
- public void draw_self_local(drawable d) {
- Color fg, bg, splash;
-
- /* pick the colors on highlight status */
- if (_highlighted) {
- bg=draw_colors().foreground();
- fg=draw_colors().background();
- } else {
- fg=draw_colors().foreground();
- bg=draw_colors().background();
- }
-
- /* clear the background if they asked for that */
- if (opaque()) {
- d.setColor(bg);
-
- /* if they are selected we need to do some work */
- if (_highlighted) {
- /* are they the focus? */
- if (_focus) {
- /* leave room for a one pixel splash border and a white pixel */
- d.fillRect(2,2,w()-4,h()-4);
- } else {
- /* leave room for only one white pixel above */
- d.fillRect(0,1,w()-1,h()-2);
- }
- } else {
- /* simple case: not highlighted just clear the whole thing */
- d.fillRect(0,0,w(),h());
- }
- }
-
- /* we only want the extra border if they are the focus */
- if (_focus) {
- /* they are highlighted, so put in the splash color border */
- splash=style_manager.default_color_scheme().splash();
- d.setColor(splash);
- /* draw it */
- d.drawRect(0,0,w()-1,h()-1);
- }
-
- /* set font to our font, and start drawing in foreground */
- d.setFont(font());
- d.setColor(fg);
-
- /* put up the string */
- d.drawString(text(), _h_spacing, _above_spacing+_metric.getAscent());
- }
-
- //had:
- //* @exception general PROPAGATED.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Return if the object is highlighted right now.
- * @return boolean true if the object is currently highlighted
- */
- public boolean highlighted() {
- return _highlighted;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This static function is a convenience for creating a Vector
- * text_list_elements from an array of strings.
- * @param String[] contents the strings to make elements out of.
- * @param Font font the font to use (or pass null for a default font).
- * @return Vector a vector suitable for use with set_contents() on
- * listbox_display.
- */
- public static Vector make_contents_vector(String[] contents, Font font) {
- Font f;
- int i;
- Vector retval=new Vector();
-
- /* make the items */
- for (i=0; i<contents.length;++i) {
- retval.addElement(new text_list_element(contents[i],font));
- }
-
- /* return it */
- return retval;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This static function is a convenience for creating a Vector
- * text_list_elements from a Vector of strings.
- *
- * @param Vector contents the strings to make elements out of.
- * @param Font font the font to use (or pass null for a default font).
- * @return Vector a vector suitable for use with set_contents() on
- * listbox_display.
- */
- public static Vector make_contents_vector(Vector contents, Font font) {
- Font f;
- int i;
- Vector retval=new Vector();
- String txt;
-
- /* make the items */
- for (i=0; i<contents.size();++i) {
-
- /* get the string out */
- txt=(String)contents.elementAt(i);
-
- /* convert to an element */
- retval.addElement(new text_list_element(txt,font));
- }
-
- /* return it */
- return retval;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Compute the preferred width for the parent.
- * @return int the size of the text string being displayed + 4 pixels of
- * border.
- */
- public int preferred_width() {
- FontMetrics metrics=manager.get_metrics(font());
- return (metrics.stringWidth(text())+4);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This variable holds our state of focusedness.
- */
- protected boolean _focus=false;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This method is used to inform the object that it has become the
- * focus of a listbox. Listbox elements should draw themselves
- * differently to indicate this demarcation. Listbox elements may
- * be the focus when they are not highlighted and vice-versa.
- */
- public void become_focus() {
-
- _focus=true;
-
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This method is used to inform the object that it is not
- * the focus any longer.
- */
- public void lost_focus() {
-
- _focus=false;
-
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This method is used to query the object about whether or
- * not it is the focus.
- * @return boolean true if this object is the focus
- */
- public boolean focused() { return _focus;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
-
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-